package com.mingqi.enetconnected.view.Dialog.tree.indicator;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.mingqi.enetconnected.view.Dialog.tree.IDynamicTreeData;

/**
 * 带滑动块的指示器
 * @author yaxi
 * @date 2018/9/18
 */
public class IndicatorView extends HorizontalScrollView{

    public static final String TAG = IndicatorView.class.getSimpleName();

    private final IndicatorAdapterDataObserver mObserver = new IndicatorAdapterDataObserver();


    private AbstractAdapter mAdapter;

    private IndicatorContainer mIndicatorContainer;

    private Context mContext;




    public IndicatorView(Context context) {
        this(context,null);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mIndicatorContainer = new IndicatorContainer(context);
        addView(mIndicatorContainer);
    }



    public void setAdapter(final AbstractAdapter adapter){
        if (adapter == null) {
            throw new NullPointerException("Adapter can't be null.");
        }

        this.mAdapter = adapter;

        mAdapter.registerAdapterDataObserver(mObserver);

        int count = mAdapter.getCount();

        for (int i = 0; i < count; i++) {
            final View indicatorView = mAdapter.getView(i, mIndicatorContainer);
            mIndicatorContainer.addItemView(indicatorView);
            if (i == 0) {
                ((TextView) indicatorView).setTypeface(Typeface.DEFAULT_BOLD);
            }

            registerClickListenerForItemChild(indicatorView);
        }

        ViewTreeObserver observer = mIndicatorContainer.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {


                int width = mIndicatorContainer.getWidth();
                Log.i(TAG,  "onGlobalLayout: mIndicatorContainer.width = " + width);

                mIndicatorContainer.collectViewWidth();
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1){
                    mIndicatorContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }else{
                    mIndicatorContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

            }
        });
    }

    private void registerClickListenerForItemChild(final View indicatorView) {
        indicatorView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int clickIndex = mIndicatorContainer.getItemChildIndex(indicatorView);
                Log.i(TAG, "onClick: " + clickIndex);
                mIndicatorContainer.smoothScrollToPosition(clickIndex);
                    if (mListener != null) {
                        mListener.onItemSelected(((IDynamicTreeData) mAdapter.getData(clickIndex)).getDataLevel());
                    }
            }
        });
    }


    public void addItemChild(int position){
        View indicatorView = mAdapter.addView(position, mIndicatorContainer);
        mIndicatorContainer.addItemView(indicatorView,position);
        registerClickListenerForItemChild(indicatorView);
    }

    public void updateItemChild(int position){
        mAdapter.getView(position,mIndicatorContainer);
        mIndicatorContainer.updateItemChild(position);
        Log.i(TAG, "updateItemChild: ");
    }

    public void removeItemChild(int position){

    }

    public void removeRangeChild(int start,int count){
        Log.i(TAG, "removeRangeChild: start = "+start +";count = " + count);
        for (int i = 0; i < count; i++) {
            mAdapter.removeView(start);
        }
        mIndicatorContainer.removeItemViews(start,count);
        invalidate();
    }



    private OnItemSelectedListener mListener;

    public interface OnItemSelectedListener{
        void onItemSelected(int level);
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        mListener = listener;
    }

    public void removeOnItemSelectedListener(){
        mListener = null;
    }


    public class IndicatorAdapterDataObserver extends AbstractAdapter.AbstractAdapterDataObserver {
        @Override
        public void onChanged() {
            super.onChanged();
        }

        @Override
        public void onItemAdd() {
            super.onItemAdd();
        }

        @Override
        public void onItemAdd(int position) {
            super.onItemAdd(position);
            addItemChild(position);
        }

        @Override
        public void OnItemChanged(int position) {
            super.OnItemChanged(position);
            updateItemChild(position);
        }

        @Override
        public void onItemRemoved(int position) {
            super.onItemRemoved(position);
            removeItemChild(position);
        }

        @Override
        public void onRangeItemRemoved(int start, int count) {
            super.onRangeItemRemoved(start, count);
            removeRangeChild(start, count);
        }
    }
}
